//
//  WQCHomeViewController.m
//  renxinghuan
//
//  Created by 王晓民 on 2018/3/16.
//  Copyright © 2018年 吴清翠. All rights reserved.
//

#import "WQCHomeViewController.h"
#import "configuration.h"
#import "WQCRepaymentViewController.h"//智能还款
#import "WQCFastViewController.h"//快速消费
#import "WQCMyPlanViewController.h"//我的计划
#import "WQCShareViewController.h"//我要分享
#import "vipModel.h"
#import "HWRollingView.h"
#import "WYCPictureModel.h"
#import "WQCMemberViewController.h"
#import "WYCShimingController.h"
#import "WYCMymessegeController.h"
#import "WYCMessegeModel.h"
#import "UIButton+WYButton.h"
#import "vipModel.h"
@interface WQCHomeViewController ()

@property(nonatomic,strong)HWRollingView * RollingView;
@property(nonatomic,strong)UILabel  *unAlreadyBillAmount;
@property(nonatomic,strong)UILabel  *AlreadyBillAmount;
@property(nonatomic,strong)NSMutableArray * imageArray;
@property(nonatomic,strong)NSMutableArray *vipArr;
@property(nonatomic,strong)NSMutableArray *vipnameArr;
@property(nonatomic,strong)NSMutableArray *vipmoneyArr;
@property(nonatomic,strong) UIView  *footview ;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong) UIButton  *mesBnt;

@property (nonatomic,strong)NSMutableArray *vipArray;

@end

@implementation WQCHomeViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden =YES;
    [self setupRequster];
    [self loadVip];
    [self setupIMG];
    [self loadProfit];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //图片数组
    self.imageArray = [[NSMutableArray alloc] init];
    self.dataArray =[[NSMutableArray alloc]init];

    
    
    self.vipArr = [[NSMutableArray alloc]init];
    self.vipnameArr = [[NSMutableArray alloc]init];
    self.vipmoneyArr =[[NSMutableArray alloc]init];
    //列表数组
    self.view.backgroundColor = WYCGlobalBg;
}
#pragma make ------->>>轮播图
- (void)setupIMG{
    [self.imageArray removeAllObjects];
    [HttpTool POST:bannerlist params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSArray * resultarray = responseObject[@"result"];
            for (NSDictionary * dict in resultarray) {
                
                WYCPictureModel * model = [WYCPictureModel mj_objectWithKeyValues:dict];
                [self.imageArray addObject:model.imgUrl];
            }
           
            [self initImageNamesSystemPageControlAndTitle];

        }else{
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}
#pragma make ------->>>轮播图
- (void)loadVip{
    
    [HttpTool POST:vipList params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSArray * resultarray = responseObject[@"result"];
            for (NSDictionary * dict in resultarray) {
               NSString  *name = dict[@"name"];
                [self.vipnameArr addObject:name];
                self.vipArray = [NSMutableArray arrayWithArray:[vipModel getVipModelFromJson:responseObject[@"result"]]];
            }
            [self creatVipView];

        }
       
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        //        [self showToast:Failed];
    }];
}
-(void)creatVipView
{
    NSLog(@"555%@",_vipnameArr);
    if (_vipnameArr.count>0) {
        UIView  *vipView = [[UIView alloc]initWithFrame:CGRectMake(0, _footview.frame.size.height+_footview.frame.origin.y+10, kScreenW, 180)];
        vipView.backgroundColor = [UIColor whiteColor];
        vipView.userInteractionEnabled = YES;
        [self.view addSubview:vipView];
        
        NSLog(@"会员%@",_vipnameArr);
//        UIImageView  *yipImage = [MyUIClass makeUIImageViewWithFrame:CGRectMake(ScreenWidth/2-95, 30, 186, 96) andImage:@"daili"];
//        [vipView addSubview:yipImage];
        for (vipModel *model in self.vipArray) {
            if ([model.isMine isEqualToString:@"0"]) {
                if ([model.grade isEqualToString:@"0"]) {
                    NSString *str =  model.name;
                    UIImageView  *yipImage = [MyUIClass makeUIImageViewWithFrame:CGRectMake(ScreenWidth/2-95, 30, 186, 96) andImage:@"mydj"];
                    [vipView addSubview:yipImage];
                    UILabel  *model = [MyUIClass makeUILabelWithFrame:CGRectMake(ScreenWidth/2-75, 100, 150, 30) andBackColor:[UIColor clearColor] andText:str andTextColor:[UIColor blackColor] andFont:[UIFont systemFontOfSize:16] andAlignment:NSTextAlignmentCenter];
                    [vipView addSubview:model];
                }
                if ([model.grade isEqualToString:@"1"]) {
                     NSString *str =  model.name;
                    UIImageView  *yipImage = [MyUIClass makeUIImageViewWithFrame:CGRectMake(ScreenWidth/2-95, 30, 186, 96) andImage:@"putong"];
                    [vipView addSubview:yipImage];
                    UILabel  *model = [MyUIClass makeUILabelWithFrame:CGRectMake(ScreenWidth/2-75, 100, 150, 30) andBackColor:[UIColor clearColor] andText:str andTextColor:[UIColor blackColor] andFont:[UIFont systemFontOfSize:16] andAlignment:NSTextAlignmentCenter];
                    [vipView addSubview:model];
                }
                if ([model.grade isEqualToString:@"2"]) {
                     NSString *str =  model.name;
                    
                    
                    UIImageView  *yipImage = [MyUIClass makeUIImageViewWithFrame:CGRectMake(ScreenWidth/2-95, 30, 186, 96) andImage:@"chaoji"];
                    [vipView addSubview:yipImage];
                    UILabel  *model = [MyUIClass makeUILabelWithFrame:CGRectMake(ScreenWidth/2-75, 100, 150, 30) andBackColor:[UIColor clearColor] andText:str andTextColor:[UIColor blackColor] andFont:[UIFont systemFontOfSize:16] andAlignment:NSTextAlignmentCenter];
                    [vipView addSubview:model];
                }
                if ([model.grade isEqualToString:@"3"]) {
                    NSString *str =  model.name;
                    UIImageView  *yipImage = [MyUIClass makeUIImageViewWithFrame:CGRectMake(ScreenWidth/2-95, 30, 186, 96) andImage:@"daili"];
                    [vipView addSubview:yipImage];
                    UILabel  *model = [MyUIClass makeUILabelWithFrame:CGRectMake(ScreenWidth/2-75, 100, 150, 30) andBackColor:[UIColor clearColor] andText:str andTextColor:[UIColor blackColor] andFont:[UIFont systemFontOfSize:16] andAlignment:NSTextAlignmentCenter];
                    [vipView addSubview:model];
                }
                
                
                
                
            }
        }
        
        
        
        
        
        UILabel  *putong = [MyUIClass makeUILabelWithFrame:CGRectMake(0, 100, 100, 30) andBackColor:[UIColor clearColor] andText:_vipnameArr[0] andTextColor:[UIColor blackColor] andFont:[UIFont systemFontOfSize:16] andAlignment:NSTextAlignmentCenter];
        [vipView addSubview:putong];
        
        UILabel  *superLb = [MyUIClass makeUILabelWithFrame:CGRectMake(ScreenWidth/2-75, 0, 150, 30) andBackColor:[UIColor clearColor] andText:_vipnameArr[1] andTextColor:[UIColor blackColor] andFont:[UIFont systemFontOfSize:16] andAlignment:NSTextAlignmentCenter];
        [vipView addSubview:superLb];
        UILabel  *daili = [MyUIClass makeUILabelWithFrame:CGRectMake(ScreenWidth-100, 100, 100, 30) andBackColor:[UIColor clearColor] andText:_vipnameArr[2] andTextColor:[UIColor blackColor] andFont:[UIFont systemFontOfSize:16] andAlignment:NSTextAlignmentCenter];
        [vipView addSubview:daili];
        
      
        //登录按钮
        UIButton * loginButton = [[UIButton alloc] init];
        loginButton.frame = CGRectMake(ScreenWidth/2-65, 135, 130, 35);
        loginButton.backgroundColor = wycMain;
        loginButton.layer.cornerRadius = 15;
        [loginButton setTitle:@"立即升级" forState:UIControlStateNormal];
        loginButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [loginButton addTarget:self action:@selector(upgradeButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [vipView addSubview:loginButton];
    }
}
/**
 *  使用本地图片、系统PageControl、设置标题
 */
- (void)initImageNamesSystemPageControlAndTitle {
    
    UIView * cellview = [[UIView alloc] init];
    cellview.frame = CGRectMake(0, 0, ScreenWidth, 750  * px);
    cellview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:cellview];
        
      _RollingView= [HWRollingView rollingViewWithFrame:CGRectMake(0, 0, ScreenWidth, 750 * px) RollingStyle:RollingStyleNetwork Data:self.imageArray];
    [cellview addSubview:_RollingView];

    _mesBnt = [UIButton buttonWithType:UIButtonTypeCustom];
    _mesBnt.frame = CGRectMake(ScreenWidth-65, kDevice_Is_iPhoneX?44:20, 50, 50);
    [_mesBnt setImage:[UIImage imageNamed:@"mesage"] forState:UIControlStateNormal];
    _mesBnt.imageView.contentMode=UIViewContentModeScaleToFill;
    [ _mesBnt addTarget:self action:@selector(pressMes) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_mesBnt];
    //创建列表
    [self setupTableView];
    
}
#pragma make ----------->>请求
- (void)setupRequster{
    
    [HttpTool POST:list params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"消息%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSDictionary * dic = responseObject[@"result"];
            NSArray * dataArray = dic[@"data"];
            if (![dataArray isKindOfClass:[NSNull class]])
            {
            for (NSDictionary * dict in dataArray) {
                WYCMessegeModel * messegeModel = [WYCMessegeModel mj_objectWithKeyValues:dict];
                [self.dataArray addObject:messegeModel];
                
                if ([messegeModel.isRead isEqualToString:@"1"]) {
                    [_mesBnt showBadgeOnItemIndex];return ;
                }else{
                    [_mesBnt hideBadgeOnItemIndex];
                }
            }
            }
        }else{
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
            [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
        }
       
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
-(void)pressMes
{
    WYCMymessegeController  *mesView = [[WYCMymessegeController alloc]init];
    [self.navigationController pushViewController:mesView animated:YES];
}
- (void)setupTableView{
    //功能栏View
    UIView * showView = [[UIView alloc] init];
    showView.backgroundColor = [UIColor whiteColor];
    showView.frame = CGRectMake(0, _RollingView.frame.size.height+_RollingView.frame.origin.y, kScreenW, 100);
    [self.view addSubview:showView];
    NSArray  *imageArr = @[@"bank_01",@"bank_03",@"bank_04"];
    NSArray  *titleArr = @[@"智能还款",@"我的计划",@"我要分享"];
    
    for (int i = 0; i<imageArr.count; i++)
    {
        
        UIButton  *btnank = [UIButton buttonWithType:UIButtonTypeCustom];
        btnank.frame = CGRectMake(kScreenW/3*i, 0, kScreenW/3, 100);
        btnank.tag = 300+i;
        [ btnank addTarget:self action:@selector(pressMain:) forControlEvents:UIControlEventTouchUpInside];
        
        [showView addSubview:btnank];
        
        UIImageView *noticeimageView = [[UIImageView alloc] init];
        noticeimageView.frame = CGRectMake((kScreenW/3-44)/2, 10, 44, 44 );
        noticeimageView.image = [UIImage imageNamed:[imageArr objectAtIndex:i]];
        [btnank addSubview:noticeimageView];
        UILabel  *banLb = [[UILabel alloc]init];
        banLb.frame = CGRectMake(0, 60, kScreenW/3, 20);
        banLb.textAlignment  = NSTextAlignmentCenter;
        banLb.textColor = [UIColor blackColor];
        banLb.text = [titleArr objectAtIndex:i];
        banLb.font = [UIFont systemFontOfSize:13];
        [btnank addSubview:banLb];
    }
   _footview= [[UIView alloc]initWithFrame:CGRectMake(0, showView.frame.size.height+showView.frame.origin.y+10, kScreenW, 80)];
    _footview.backgroundColor = [UIColor whiteColor];
    _footview.userInteractionEnabled = YES;
    [self.view addSubview:_footview];
    
    
    //已还账单
    _AlreadyBillAmount = [[UILabel alloc] init];
    _AlreadyBillAmount.frame = CGRectMake(20,20, 100, 20);
    _AlreadyBillAmount.textAlignment = NSTextAlignmentCenter;
    _AlreadyBillAmount.textColor = [UIColor blackColor];
    _AlreadyBillAmount.font = [UIFont boldSystemFontOfSize:20];
    _AlreadyBillAmount.text = @"0.00";
    [_footview addSubview:_AlreadyBillAmount];
    UILabel  *yuan = [[UILabel alloc] init];
    yuan.frame = CGRectMake(30,_AlreadyBillAmount.frame.size.height+_AlreadyBillAmount.frame.origin.y , 15, 15);
    yuan.backgroundColor = wycMain;
    yuan.layer.masksToBounds = YES;
    yuan.textAlignment = NSTextAlignmentCenter;
    yuan .layer.cornerRadius = 14;
    [_footview addSubview:yuan];
    UILabel  *yuan3 = [[UILabel alloc] init];
    yuan3.frame = CGRectMake(50, _AlreadyBillAmount.frame.size.height+_AlreadyBillAmount.frame.origin.y, 80, 20);
    yuan3.textColor = [UIColor blackColor];
    yuan3.text = @"今日收益";
    yuan3.font = [UIFont systemFontOfSize:13];
    yuan3.textAlignment = NSTextAlignmentCenter;
    [_footview addSubview:yuan3];
    
    //未还账单
    _unAlreadyBillAmount = [[UILabel alloc] init];
    _unAlreadyBillAmount.frame = CGRectMake(kScreenW-120,20, 100, 20);
    _unAlreadyBillAmount.textAlignment = NSTextAlignmentCenter;
    _unAlreadyBillAmount.textColor = [UIColor blackColor];
    _unAlreadyBillAmount.font = [UIFont boldSystemFontOfSize:20];
    _unAlreadyBillAmount.text = @"0.00";
    [_footview addSubview:_unAlreadyBillAmount];
    UILabel  *yuan1 = [[UILabel alloc] init];
    yuan1.frame = CGRectMake(kScreenW-130, _unAlreadyBillAmount.frame.size.height+_unAlreadyBillAmount.frame.origin.y , 15, 15);
    yuan1.backgroundColor = [UIColor lightGrayColor];
    yuan1.layer.masksToBounds = YES;
    yuan1.textAlignment = NSTextAlignmentCenter;
    yuan1 .layer.cornerRadius = 14;
    [_footview addSubview:yuan1];
    UILabel  *yuan2 = [[UILabel alloc] init];
    yuan2.frame = CGRectMake(kScreenW-110, _unAlreadyBillAmount.frame.size.height+_unAlreadyBillAmount.frame.origin.y, 80, 20);
    yuan2.textColor = [UIColor blackColor];
    yuan2.text = @"累计收益";
    yuan2.font = [UIFont systemFontOfSize:13];
    yuan2.textAlignment = NSTextAlignmentCenter;
    [_footview addSubview:yuan2];
    
}
-(void)upgradeButtonClick
{
    WQCMemberViewController *superView = [[WQCMemberViewController alloc]init];
    [self.navigationController pushViewController:superView animated:YES];
}
-(void)pressMain:(UIButton *)sender
{
    
    if(sender.tag == 300)
    {
        NSLog(@"智能还款");
        NSString  *auth = [WYCAccountTool unarchiveisAuth];
        if ([auth isEqualToString:@"0"]) {
        WQCRepaymentViewController *huanView = [[WQCRepaymentViewController alloc]init];[self.navigationController pushViewController:huanView animated:YES];
        }
        else
        {
            WYCShimingController * shiming = [[WYCShimingController alloc] init];
            [self.navigationController pushViewController:shiming animated:NO];
        
    }
    }
   
    else if (sender.tag == 301)
    {
        NSLog(@"我的计划");
        NSString  *auth = [WYCAccountTool unarchiveisAuth];
        if ([auth isEqualToString:@"0"]) {
        WQCMyPlanViewController *myplanVC = [[WQCMyPlanViewController alloc] init];
        [self.navigationController pushViewController:myplanVC animated:YES];
        }
        else
        {
            WYCShimingController * shiming = [[WYCShimingController alloc] init];
            [self.navigationController pushViewController:shiming animated:NO];
        }
    }
    else if (sender.tag == 302)
    {
        NSLog(@"我要分享");
        NSString  *auth = [WYCAccountTool unarchiveisAuth];
        if ([auth isEqualToString:@"0"]) {
        WQCShareViewController *shareVC = [[WQCShareViewController alloc] init];
        [self.navigationController pushViewController:shareVC animated:YES];
        }
        else
        {
            WYCShimingController * shiming = [[WYCShimingController alloc] init];
            [self.navigationController pushViewController:shiming animated:NO];
        }
        
    }
}
//我的收益
-(void)loadProfit
{
    [HttpTool POST:myProfit params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            
            NSDictionary *result = [responseObject objectForKey:@"result"];
            _unAlreadyBillAmount.text =result[@"allShare"];
            _AlreadyBillAmount.text =result[@"yesterdayShare"];
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
